<!DOCTYPE html>
<html>
<head>
    <title>添加公众号</title>
    {% include 'admin/common/header.html' %}
</head>
<body>
<form class="layui-form">
    <div class="mainBox">
        <div class="main-container">
            <div class="main-container">
                <div class="layui-form-item">
                    <label class="layui-form-label">公众号</label>
                    <div class="layui-input-block">
                        <input type="text" name="wx_name" lay-verify="title" autocomplete="off" placeholder="请输入公众号名称"
                               class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">区域分组</label>
<!--                    <div class="layui-input-block">-->

<!--                         <select id="wx_group_code" name="wx_group_code" lay-verify="required" lay-filter="wx-group-code">-->
<!--                            </select>-->

<!--                    </div>-->

                     <div class="layui-input-block" id="wx_group_ids" name="wx_group_ids">
                        <input type="checkbox" name="like[write]" title="写作">
                        <input type="checkbox" name="like[read]" title="阅读" checked>
                        <input type="checkbox" name="like[dai]" title="发呆">
                     </div>

                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">采集机器人</label>
                    <div class="layui-input-block">
<!--                        <input type="text" name="robot_id" lay-verify="title" autocomplete="off" placeholder="请输入采集机器人"-->
<!--                               class="layui-input">-->
                            <select id="robot_id" name="robot_id" lay-verify="required" lay-filter="robotId">
<!--                                <option value="">&#45;&#45;请选择&#45;&#45;</option>-->
                            </select>
                    </div>
                </div>


            </div>
        </div>
    </div>
    <div class="bottom">
        <div class="button-container">
            <button type="submit" class="pear-btn pear-btn-primary pear-btn-sm" lay-submit="" lay-filter="OffAcc-save">
                <i class="layui-icon layui-icon-ok"></i>
                提交
            </button>
            <button type="reset" class="pear-btn pear-btn-sm">
                <i class="layui-icon layui-icon-refresh"></i>
                重置
            </button>
        </div>
    </div>
</form>
{% include 'admin/common/footer.html' %}
<script>


  layui.use(['form', 'jquery'], function () {
    let form = layui.form
    let $ = layui.jquery

    //渲染公众号分组下拉选择框
    $.ajax({
            url: "/admin/group/data",
            type : "GET",
            contentType: "application/json;charset=utf-8",
            dataType : "json",
            success:function(data) {
                    $("#wx_group_ids").empty();
                    // 此处添加默认的选择(如果没有这个option，则ios系统可能无法打开下拉框，所以为了(照顾)他们，此处还是有必要的)
                    //$("#wx_group_code").append("<option value='' selected disabled>" +"----请选择----"+"</option>")
                    //for (var i = 0; i < data.data.length; i++) {
                    //    $("#wx_group_code").append("<option value=" + data.data[i].code  + ">" +data.data[i].name +"</option>")
                    //}
                    for (var i = 0; i < data.data.length; i++) {
                          $("#wx_group_ids").append('<input type="checkbox" name="group" value="'+data.data[i].id+'" title="'+data.data[i].name+'">')
                    }

                    // 注意 没有这个渲染，可能会回填失败
                    //layui.form.render('select');
                    layui.form.render('checkbox');
            }
        })



    //渲染机器人下拉选择框
    $.ajax({
            url: "/admin/robot/data",
            type : "GET",
            contentType: "application/json;charset=utf-8",
            dataType : "json",
            success:function(data) {
                    // 此处每次打开 做一次清空操作，防止记忆上次的选择
                    $("#robot_id").empty();
                    // 此处添加默认的选择(如果没有这个option，则ios系统可能无法打开下拉框，所以为了(照顾)他们，此处还是有必要的)
                    $("#robot_id").append("<option value='' selected disabled>" +"----请选择----"+"</option>")
                    for (var i = 0; i < data.data.length; i++) {
                        $("#robot_id").append("<option value=" + data.data[i].id  + ">" +data.data[i].name +"</option>")
                    }
                    // 注意 没有这个渲染，可能会回填失败
                    layui.form.render('select');
            }
        })
    //取值
    //$("#robot_id").val();

    form.on('submit(OffAcc-save)', function (data) {
      var labelArray = $('input[name="group"]');
      var labels = [];
      for (k in labelArray){
       if (labelArray[k].checked){
           labels.push(labelArray[k].value);
       }
      }
      data.field.groups = labels;
      $.ajax({
        url: '/admin/wxOfficialAcc/offAcc/save',
        data: JSON.stringify(data.field),
        dataType: 'json',
        contentType: 'application/json',
        type: 'post',
        success: function (result) {
          if (result.success) {
            layer.msg(result.msg, { icon: 1, time: 1000 }, function () {
              parent.layer.close(parent.layer.getFrameIndex(window.name))//关闭当前页
              //parent.layui.table.reload('user-table')
            })
          } else {
            layer.msg(result.msg, { icon: 2, time: 1000 })
          }
        }
      })
      return false
    })
  })
</script>
<script>
</script>
</body>
</html>